package com.yuexun.beilunpatient.ui.satisfaction.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.satisfaction.adapter.SatisfactionListAdapter;
import com.yuexun.beilunpatient.ui.satisfaction.bean.SatisfactionDateBean;
import com.yuexun.beilunpatient.ui.satisfaction.model.impl.SatisfactionModel;
import com.yuexun.beilunpatient.ui.satisfaction.presenter.impl.SatisfactionPresenter;
import com.yuexun.beilunpatient.ui.satisfaction.ui.Act_Satisfaction_Start;
import com.yuexun.beilunpatient.ui.satisfaction.ui.view.ISatisfactionView;
import com.yuexun.beilunpatient.widget.EmptyLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/4/12 0012.
 */

public class Frag_Satisfaction_DraftBox extends KJFragment implements ICallBackListener, ISatisfactionView {
    @Bind(R.id.content_list)
    PullToRefreshList contentList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private SatisfactionListAdapter adapter;
    SatisfactionPresenter presenter;
    private int nowpage = 1;
    private int pagesize = 20;
    List<SatisfactionDateBean> dateBeans = new ArrayList<>();
    private String typeId = "";
    private String areaId = "";
    private String hospId = "";
    private String key = "";

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_list, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        presenter = new SatisfactionPresenter(new SatisfactionModel(), this);
        listViewPreference();
        initPresenter();
    }

    private void initPresenter(){
        Map<String, String> params = new HashMap<>();
        params.put("inquireKey", key);
        params.put("hospId", hospId);
        params.put("areaId", areaId);
        params.put("inquireType", "0");//状态0草稿箱 1我提交的 2我收到的
        params.put("typeId", typeId);
        params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", nowpage + "");
        params.put("pageSize", pagesize + "");
        presenter.inquireUserSurveyPageList(params);
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        ListView mList = contentList.getRefreshView();
//        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        contentList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) contentList.getFooterLoadingLayout()).setNoMoreDataText("已经是最新状态");

        adapter = new SatisfactionListAdapter(mList, dateBeans, R.layout.item_survey_draftbox);
        mList.setAdapter(adapter);

        adapter.setBackListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {
                Intent intent = new Intent(getActivity(), Act_Satisfaction_Start.class);
                intent.putExtra("type", "get");
                intent.putExtra("templateId", dateBeans.get(bundle.getInt("position")).getTemplateId());
                intent.putExtra("satisfactionId", dateBeans.get(bundle.getInt("position")).getSatisfactionId());
                intent.putExtra("createName", dateBeans.get(bundle.getInt("position")).getCreateName());
                startActivity(intent);
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(getActivity(), Act_Satisfaction_Start.class);
                intent.putExtra("type", "get");
                intent.putExtra("templateId", dateBeans.get(position).getTemplateId());
                intent.putExtra("satisfactionId", dateBeans.get(position).getSatisfactionId());
                intent.putExtra("createName", dateBeans.get(position).getCreateName());
                startActivity(intent);
            }
        });

        contentList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                initPresenter();
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                initPresenter();
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                initPresenter();
            }
        });
    }

    @Override
    public void callback(Bundle bundle) {
        if(bundle != null){
            key = bundle.getString("key");
            typeId = bundle.getString("typeId");
            areaId = bundle.getString("areaId");
            hospId = bundle.getString("hospId");
            nowpage = 1;
            initPresenter();
        }
    }

    @Override
    public void showSatisfactionPageList(BaseEntity<SatisfactionDateBean> entity) {
        if (contentList.isPullRefreshEnabled()) {
            contentList.onPullDownRefreshComplete();
        }
        if (entity.getStatus().equals("200")) {
            if (entity.getDatas().size() == 0) {
                contentList.setHasMoreData(false);
            } else {
                if (contentList.isPullLoadEnabled()) {
                    contentList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                dateBeans.clear();
                dateBeans = entity.getDatas();
            } else {
                dateBeans.addAll(entity.getDatas());
            }
            adapter.refresh(dateBeans);
        }else {
            ViewInject.toast(entity.getMessage());
        }

        if (dateBeans.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
